<template>
	<view class="beijingse ">
		<header-top :messageInfo="{typeFlag:true,message:'确认订单',liner:true}"></header-top>
		<view class="beijingtu"></view>
		<view class="shangti mainpadding">
			<!-- 选择收货地址 -->
			<view class=" ffffff radius xzshdz" @click="tjshdz" v-if="!dingdan.addressinfo">
				<view class="bigtext textcenter">点击添加收货地址</view>
			</view>
			<!-- 收货地址 -->
			<view class="flexbetween ffffff radius mainpadding" @click="tjshdz" v-if="dingdan.addressinfo">
				<view class="">
					<view class="flexleft">
						<view class="bigtext margin_right3">{{dingdan.addressinfo.name}}</view>
						<view class="bigtext">{{dingdan.addressinfo.mobile}}</view>
					</view>
					<view class="margin_top2 huisetext">
						{{dingdan.addressinfo.cityinfo}}{{dingdan.addressinfo.address_detail}}
					</view>
				</view>
				<view class="huiyoujt"></view>
			</view>
			<!-- 产品 -->
			<view class="flexbetween mainpadding ffffff radius margin_top" v-for="item in dingdan.goodsList"
				:key="item.id">
				<view class="shilitu">
					<image :src="item.image_text" mode=""></image>
				</view>
				<view class="youbox">
					<view class="titletext xiankuan">{{item.name}}</view>
					<view class="margin_top2 flexbetween">
						<view class="honhsetext">{{item.price}}</view>
						<view class="jiajian">
							<view class="jianhao" @click="jianjian(item)"></view>
							<view class="xhtext">{{item.buy_num}}</view>
							<view class="jiahao" @click="jiajia(item)"></view>
						</view>
					</view>
				</view>
			</view>
			<!-- 备注 -->
			<view class=" ffffff radius mainpadding margin_top">
				<view class="ershil">下单提醒</view>
				<view class="margin_top2 huisetext">下单证件、收货人、激活人必须是同一个人下单证件、收
					货人、激活人必须是同一个人下单证件、收货人、激活人必须是同一个人</view>
			</view>
			<!-- 输入框 -->
			<view class="mainpadding ffffff radius flexbetween margin_top">
				<view class="flexleft">
					<view class="ershil margin_right1">名字</view>
					<view class="xinghao">*</view>
				</view>
				<input class="inputs" type="text" placeholder="请输入名字" v-model="name">
			</view>
			<view class="mainpadding ffffff radius flexbetween margin_top">
				<view class="flexleft">
					<view class="ershil margin_right1">身份证号</view>
					<view class="xinghao">*</view>
				</view>
				<input class="inputs" type="text" placeholder="请输入身份证号" v-model="sf_code">
			</view>
			<view class="mainpadding ffffff radius flexbetween margin_top">
				<view class="flexleft">
					<view class="ershil margin_right1">备注</view>
					<!-- <view class="xinghao">*</view> -->
				</view>
				<input class="inputs" type="text" placeholder="请输入留言信息给卖家" v-model="note">
			</view>
		</view>
		<!-- 固定底部 -->
		<view class="gudingdb ffffff">
			<view class="flexbetween">
				<view class=" flexleft">
					<view class="notitletext margin_right1">合计：</view>
					<view class="bigqianshu">¥{{allprice}}</view>
				</view>
				<view class="jsbtn" @click="atoncePay">立即支付</view>
			</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				goodsinfo: [], //商品数据
				type: "cart",
				dingdan: {},
				allprice: "0.00", //总钱数
				address_id: 0,
				name: "",
				sf_code: "",
				note: "",
			}
		},
		watch: {
			"dingdan.goodsList": {
				deep: true,
				handler(newval) {
					let num = 0
					newval.forEach(item => {
						num = Number(num) + Number(item.price) * Number(item.buy_num)
					})
					this.allprice = num.toFixed(2);
				}
			}
		},
		onLoad(options) {
			let _this = this;
			let data = JSON.parse(decodeURIComponent(options.msg));
			console.log(data, "跳转界面接收参数")
			this.goodsinfo = data.goodsinfo
			this.type = data.type
			console.log(this.goodsinfo, "商品数据")
			// _this.getdata(options.id);
		},
		onShow() {
			uni.$off("addressId")
			uni.$on("addressId", data => {
				console.log(data, "fadhaidshfak1121")
				this.address_id = data
			})
			this.ddqry()
		},
		methods: {


			// 加
			jiajia(item) {
				// item.buy_num = item.buy_num+1
				item.buy_num = Number(item.buy_num) + 1
			},
			// 减
			jianjian(item) {
				if (item.buy_num <= 1) {
					return
				}
				item.buy_num = Number(item.buy_num) - 1
			},
			// 订单确认订单页
			ddqry() {
				httpRequest.request('api/order/order', 'POST', {
					// 传参
					data: this.goodsinfo,
					address_id: this.address_id
				}, false, false, true).then(res => {
					if (res.code == 1) {
						this.dingdan = res.data
						if (res.data.addressinfo) {
							this.address_id = res.data.addressinfo.id
						}
					}
				})
			},
			// 跳转收货地址页面
			tjshdz() {
				uni.navigateTo({
					url: '/pages/homepage/shouhuodizhi?type=' + 1
				})
			},
			atoncePay() {
				uni.showLoading({
					title: '请稍候...',
					mask: true
				})
				if (this.name == "") {
					uni.hideLoading()
					httpRequest.toast("请输入姓名")
					
					return
				}
				if (this.sf_code == "") {
					uni.hideLoading()
					httpRequest.toast("请输入身份证号")
					return
				}
				let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
				if (reg.test(this.sf_code) === false) {
					uni.hideLoading()
					httpRequest.toast("请输入正确的身份证号")
					return false;
				}
				let data = []
				this.dingdan.goodsList.forEach(item => {
					data.push({
						goodsId: item.id,
						num: item.buy_num
					})
				})
				httpRequest.request('api/order/setUpOrder', 'POST', {
					// 传参
					fromType: this.type,
					data: data,
					note: this.note,
					username: this.name,
					card_no: this.sf_code,
					address_id: this.address_id,
					order_from: "wx",
					pay_type: 10
				}, false, false, true).then(res => {
					if (res.code == 1) {
						this.orderPay(res.data.order_id)
					} else {
						httpRequest.toast(res.msg)
					}
				})
			},
			orderPay(id) {
				httpRequest.request('api/order/orderPay', 'POST', {
					// 传参
					order_id: id
				}, false, false, true).then(res => {
					if (res.code == 1) {
						let pay = res.data.wxconfig
						uni.hideLoading()
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: pay.timeStamp,
							nonceStr: pay.nonceStr,
							package: pay.package,
							signType: pay.signType,
							paySign: pay.paySign,
							success: function(res) {
								// 支付成功
								setTimeout(() => {
									uni.navigateBack(1)
								}, 1000)
							},
							fail: function(err) {
								// 支付失败
							}
						});
					} else {
						httpRequest.toast(res.msg)
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.ershil {
		font-size: 26rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #333333;
	}

	.xzshdz {
		padding: 62rpx 30rpx;
	}

	.jsbtn {
		border-radius: 120rpx;
		background-color: #f75836;
		width: 250rpx;
		height: 76rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #FFFFFF;
	}

	.bigqianshu {
		font-size: 36rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #FD463E;
	}

	.inputs {
		width: 72%;
		font-size: 26rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}

	.xinghao {
		font-size: 26rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #FD463E;
	}

	.jiahao {
		background-size: 100% 100%;
		width: 21rpx;
		height: 19rpx;
		position: absolute;
		top: 14rpx;
		right: 12rpx;
		background-image: url('../../static/image/system/jiahao.png');
	}

	.jianhao {
		width: 22rpx;
		position: absolute;
		top: 20rpx;
		left: 13rpx;
		height: 3.5rpx;
		background-color: #999999;
	}

	.xhtext {
		font-size: 31rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #FD463E;
	}

	.jiajian {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 164rpx;
		height: 46rpx;
		position: relative;
		border-radius: 20rpx;
		background-size: 100% 100%;
		background-image: url('../../static/image/system/jiajian.png');
	}

	.honhsetext {
		font-size: 30rpx;
		font-family: DIN Next LT Pro-Bold, DIN Next LT Pro;
		font-weight: bold;
		color: #FD463E;
	}

	.youbox {
		width: 74%;
	}

	.shilitu {
		width: 22%;
		height: 128rpx;
		border-radius: 14rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.shangti {
		position: relative;
		top: -228rpx;
	}

	.huisetext {
		font-size: 26rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}
</style>
